package ar.com.photo_admin.service

import javax.annotation.PostConstruct

import ar.com.photo_admin.domain.*
import org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib
import org.codehaus.groovy.grails.web.mapping.LinkGenerator

/**
 * Service for domain class Notification
 */
class NotificationsService extends BaseService {

	def customMailService
	
	LinkGenerator grailsLinkGenerator
	
	/**
	 * Notifies the photographer and the client that a new purchase order has been created.
	 */
	def notifyNewOrder(PurchaseOrder purchaseOrder) {
		
		// System notifications.
		createNotificationOrder(getMessage("notification.order.confirmed.photographer"), purchaseOrder.photographer, purchaseOrder)
		createNotificationOrder(getMessage("notification.order.confirmed.client"), purchaseOrder.client, purchaseOrder)
		
		// Mail notifications.
		if (purchaseOrder.photographer.notificationsConfiguration.mailNewOrder)
			customMailService.notifyNewOrder(purchaseOrder, true)
		
		if (purchaseOrder.client.notificationsConfiguration.mailNewOrder)
			customMailService.notifyNewOrder(purchaseOrder, false)
	}
	
	/**
	 * Notifies the sender and the receiver of the message.
	 */
	def notifyNewMessage(Message message, UserMessage userMessage) {
		
		def url = getMessageUrl(message)
		
		// System notifications.
		createNotification(getMessage("messages.sent.success"), message.from, url)
		createNotification(getMessage("messages.received.success"), userMessage.to, url)
		
		message.from.save(flush:true)
		userMessage.to.save(flush:true)
		
		// Mail notifications.
		if (userMessage.to.notificationsConfiguration.mailNewMessage)
			customMailService.notifyNewMessage(message, url)
	}
	
	/**
	 * Notifies the photographer and the client of a purchase that the status of the order has changed.
	 */
	def notifyOrderStatusChange(PurchaseOrder purchaseOrder){

		// System notifications. 
		createNotificationOrder(getMessage("notification.order.status.changed.photographer"), purchaseOrder.photographer, purchaseOrder)			
		createNotificationOrder(getMessage("notification.order.status.changed.client"), purchaseOrder.client, purchaseOrder)

		// Mail notifications.
		if (purchaseOrder.photographer.notificationsConfiguration.mailOrderStatusChange)
			customMailService.notifyOrderStatusChange(purchaseOrder, true)

		if (purchaseOrder.client.notificationsConfiguration.mailOrderStatusChange)
			customMailService.notifyOrderStatusChange(purchaseOrder, false)
	}
	
	/**
	 * Creates a notification for a purchase order.
	 */
	def createNotificationOrder(description, user, purchaseOrder) {
		if (user.notificationsConfiguration.systemNewOrder)
			createNotification(description, [user], getOrderUrl(purchaseOrder))
	}
	
	/**
	 * Creates a notification with the given description and URL, and associates it with the users. 
	 */
	def createNotification(description, users, url) {
		def notification = new Notification(description:description, associatedUrl: url)
		
		notification.save(flush:true)
		
		users.each {
			notification.addToUserNotifications(it)
		}	
	}
	
	/**
	 * Creates an URL for the given purchase order.
	 */
	def getOrderUrl(purchaseOrder) {
		return grailsLinkGenerator.link(controller: 'purchaseOrder', action: 'show', id: purchaseOrder.id)
	}
	
	/**
	 * Creates an URL for the given message.
	 */
	def getMessageUrl(message) {
		return grailsLinkGenerator.link(controller: 'message', action: 'show', id: message.id)
	}
}
